<template>
  <div class="app-container home">
    <div class="concent">
      <!-- <div class="toprow">
        <h2>检测进度</h2>
        <el-button type="text" style="color: #aa2299">刷新</el-button>
      </div> -->
      <div class="rowbtn">
        <div class="left">
          <!-- 分享按钮 -->
          <div class="sambtn" @click="share">
            <img src="@/assets/images/sample/Slice 20@2x.png" alt="" />
            分享
          </div>
          <el-button-group>
            <el-button
              :class="type == 1 ? 'active button' : 'button'"
              @click="typeClick(1, 'PGTA')"
              >PGT-A</el-button
            >
            <el-button
              :class="type == 2 ? 'active button' : 'button'"
              @click="typeClick(2, 'PGTM')"
              >PGT-M</el-button
            >
            <el-button
              :class="type == 3 ? 'active button' : 'button'"
              @click="typeClick(3, 'PGTSR')"
              >PGT-SR</el-button
            >
            <el-button
              :class="type == 4 ? 'active button' : 'button'"
              @click="typeClick(4, 'NICS')"
              >NICS</el-button
            >

            <el-button
              :class="type == 5 ? 'active button' : 'button'"
              @click="typeClick(5, '进度注释')"
            >进度注释</el-button
            >
          </el-button-group>
        </div>
        <div class="right">
          <el-input
            style="width: 250px"
            v-model="queryParams.searchValue"
            placeholder="送检单位/姓名/送检医生"
            clearable
          />
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </div>
      </div>
      <div class="body">
        <el-col :span="24" :xs="24">
          <el-table
            :data="itemList"
            height="calc(100vh - 280px)"
            @selection-change="handleSelectionChange"
            border>
            <el-table-column
              type="selection"
              sortable
              :selectable="handleSelectable"
            >
            </el-table-column>
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-table :data="scope.row.sampleList">
                  <el-table-column
                    v-for="(item, ind) in allData[type].datalist"
                    :key="ind"
                    :label="item.label"
                    :prop="item.prop"
                    show-overflow-tooltip
                    align="center"
                  ></el-table-column>
                </el-table>
              </template>
            </el-table-column>
            <el-table-column
              label="序号"
              align="center"
              type="index"
              width="80px"
              sortable
            ></el-table-column>
            <el-table-column
              v-for="(item, ind) in allData[type].headlist"
              :key="ind"
              :label="item.label"
              :prop="item.prop"
              show-overflow-tooltip
              align="center"
            >
              <template slot-scope="scope">
                <el-progress
                  type="line"
                  v-if="item.prop === 'reportSchedule'"
                  :percentage="scope.row.reportSchedule"
                  :stroke-width="15"
                  :text-inside="true"
                  >
                ></el-progress>
                <span
                  v-else-if="
                    item.prop === 'reportAddress' &&
                    scope.row[item.prop] != null
                  "
                  style="color: #aa2299"
                >
                  <a :href="scope.row.reportAddress" target="_self">
                    <i class="el-icon-download"></i>
                    <span>下载</span>
                  </a>
                </span>
                <span v-else-if="item.prop != 'patientName'">{{
                  scope.row[item.prop]
                }}</span>
                <div
                  v-else-if="item.prop == 'patientName'"
                  :style="getCellColor(scope.row)"
                >
                  {{ scope.row[item.prop] }}
                </div>
              </template>
            </el-table-column>

            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['check:item:edit']"
                  >修改</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </div>
    </div>
    <el-dialog title="分享" :visible.sync="isOpen">
      <el-form :model="form" ref="form" :rules="rules">
        <el-form-item label="所属医院" :label-width="formLabelWidth">
          <el-input
            style="width: 220px"
            :value="dept.deptName"
            readonly
          ></el-input>
        </el-form-item>
        <el-form-item
          label="选择医生"
          :label-width="formLabelWidth"
          prop="userId"
        >
          <el-select v-model="form.userId" placeholder="请选择医生">
            <el-option
              v-for="user in userOptions"
              :key="user.userId"
              :label="user.nickName"
              :value="user.userId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="button" @click="shareConfirm"
          >确 定</el-button
        >
      </div>
    </el-dialog>



    <!-- 进度注释 -->
    <el-dialog :title="schedule.title"
               :visible.sync="schedule.open"
               width="80%"
               append-to-body>
      <div>
          <div>{{schedule.PGTA.title}}</div>
          <div style="margin-top: 5px">{{schedule.PGTA.content}}</div>
      </div>

      <div style="margin-top: 20px">
        <div style="margin-top: 5px">{{schedule.PGTSR.title}}</div>
        <div style="margin-top: 5px">{{schedule.PGTSR.title1}}</div>
        <div style="margin-top: 5px">{{schedule.PGTSR.content1}}</div>
        <div style="margin-top: 5px">{{schedule.PGTSR.title2}}</div>
        <div style="margin-top: 5px">{{schedule.PGTSR.content2}}</div>
      </div>

      <div style="margin-top: 20px">
        <div style="margin-top: 5px">{{schedule.PGTM.title}}</div>
        <div style="margin-top: 5px">{{schedule.PGTM.title1}}</div>
        <div style="margin-top: 5px">{{schedule.PGTM.content1}}</div>
        <div style="margin-top: 5px">{{schedule.PGTM.title2}}</div>
        <div style="margin-top: 5px">{{schedule.PGTM.content2}}</div>
      </div>

      <div style="margin-top: 20px">
        <div style="margin-top: 5px">{{schedule.NICS.title}}</div>
        <div style="margin-top: 5px">{{schedule.NICS.content}}</div>
      </div>
    </el-dialog>

    <!-- 修改   -->
    <el-dialog title="修改信息" :visible.sync="isItemOpen">
      <el-form :model="itemForm" ref="itemForm" :rules="itemRules">
        <el-form-item label="所属医院" :label-width="formLabelWidth">
          <el-input
            style="width: 220px"
            :value="itemForm.sjdwName"
            readonly
          ></el-input>
        </el-form-item>
        <el-form-item
          label="选择医生"
          :label-width="formLabelWidth"
          prop="sharedIds"
        >
          <el-select v-model="itemForm.sjys" placeholder="请选择医生">
            <el-option
              v-for="user in userOptions"
              :key="user.nickName"
              :label="user.nickName"
              :value="user.nickName"
            ></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="button" @click="itemConfirm"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getItemListBySampleDecWay2,
  shareItems,
  updateItem,
} from "@/api/check/item";
import {
  getUserListByDeptId,
  getUserListByDeptName,
} from "@/api/system/custom";

import alldata from "./item2.js";

export default {
  name: "Index",
  data() {
    return {
      // 进度
      schedule: {
        // 是否显示弹出层（客户导入）
        open: false,
        // 弹出层标题（客户导入）
        title:"进度注释",
        PGTA:{
          title:"PGT-A模块",
          content:"收样--项目运营--单细胞扩增成库--上机测序-数据分析--报告审核-完结"
        },
        PGTSR:{
          title:"PGT-SR模块",
          title1:"PGT-SR/MaReCs1",
          content1:"收样--项目运营--单细胞扩增成库--上机测序--数据分析--报告审核-完结",
          title2:"MaReCs2",
          content2:"收样--项目运营--提取--建库--上机测序-数据分析--报告审核-完结"
        },
        PGTM:{
          title:"PGT-M模块",
          title1:"PGTMF",
          content1:"收样--项目运营--提取--建库--数据分析--报告审核--完结",
          title2:"PGTM",
          content2:"收样--项目运营--单细胞扩增--建库--上机测序--生信分析--报告审核-完结"
        },
        NICS:{
          title:"NICS模块",
          content:"收样--项目运营--NICSinst成库--上机测序-数据分析--报告审核-完结"
        }

      },


      // 设置上级参数
      superior: {
        // 是否显示弹出层（客户导入）
        open: false,
        // 弹出层标题（客户导入）
        title: "",
      },


      customColor: '#aa2299',
      isItemOpen: false,
      formLabelWidth: "100px",
      // 版本号
      version: "3.8.5",
      type: 1,
      isOpen: false,
      formLabelWidth: "100px",
      form: {},
      // 总条数
      total: 0,
      // 用户表格数据
      itemList: [],
      // 查询参数
      queryParams: {
        searchValue: undefined,
        pageNum: 1,
        pageSize: 10,
        sampleDecWay: "PGTA",
      },

      status: false,
      allData: [],
      user: {},
      dept: {},
      userOptions: [],

      itemForm: {
        sjdwName: undefined,
        patientPhone: undefined,
        nickName: undefined,
        id: undefined,
      },

      // 表单校验
      rules: {
        userId: [{ required: true, message: "医生不能为空", trigger: "blur" }],
      },

      itemRules: {
        patientPhone: [
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "手机号格式错误",
            trigger: "blur",
          },
        ],
      },
    };
  },

  created() {
    this.allData = alldata;
    this.getList();
    this.getDept();
  },
  mounted() {
    this.allData = alldata;
  },

  methods: {
    getUserList() {
      var that = this;
      var form = {};
      form.deptName = that.itemForm.sjdwName;
      getUserListByDeptName(form).then((response) => {
        if (response.code == "200" && response.data) {
          that.userOptions = response.data;
        }
      });
    },
    itemConfirm: function () {
      var that = this;
      that.$refs["itemForm"].validate((valid) => {
        if (valid) {
          var form = that.itemForm;
          updateItem(form).then((response) => {
            //需要判断当前传入的userId是否有deptId，没有的话提示
            this.$modal.msgSuccess("修改成功");
            this.isItemOpen = false;
            that.getList();
          });
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.isItemOpen = true;
      this.itemForm.sjdwName = row.sjdwName;
      this.title = "修改项目信息";
      this.itemForm.id = row.id;
      this.getUserList();
    },
    reset() {
      this.itemForm = {
        sjdwName: undefined,
        patientPhone: undefined,
        nickName: undefined,
        id: undefined,
      };
    },

    getCellColor(row) {
      var nickName = this.$store.state.user.nickName;
      if (row.sjys != nickName && row.isShare && row.isShare == 1) {
        return {
          background: "#aa2299",
          color: "#ffffff",
        };
      }
    },

    handleSelectable(row) {
      if (row.sjys == this.$store.state.user.nickName) {
        return true;
      } else {
        return false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.searchValue = undefined;
      this.handleQuery();
    },
    share: function () {
      var that = this;
      var ids = this.ids;
      if (ids == null || ids.length == 0) {
        this.$modal.msgError("请先选择要分享的数据");
        return false;
      } else {
        that.isOpen = true;
      }
    },

    shareConfirm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          var userId = this.form.userId;
          var form = {};
          form.sharedId = this.form.userId;
          form.deptId = this.dept.deptId;
          form.ids = this.ids;
          shareItems(form).then((response) => {
            //需要判断当前传入的userId是否有deptId，没有的话提示
            this.$modal.msgSuccess("分享成功");
            this.isOpen = false;
          });
        }
      });
    },

    getDept() {
      var that = this;
      var dept = this.$store.state.user.dept;
      if (dept != null || dept != undefined) {
        var deptName = dept.deptName;
        var deptId = dept.deptId;
        that.dept.deptName = deptName;
        that.dept.deptId = deptId;
        that.getUser(deptId);
      }
    },

    getUser(deptId) {
      var that = this;
      var form = {};
      form.deptId = deptId;
      that.user.nickName = this.$store.state.user.nickName;
      var userId = this.$store.state.user.userId;
      form.userId = userId;
      that.user.userId = userId;
      getUserListByDeptId(form).then((response) => {
        if (response.code == "200" && response.data) {
          that.userOptions = response.data;
        }
      });
    },

    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      getItemListBySampleDecWay2(this.queryParams).then((response) => {
        this.itemList = response.rows;
        this.total = response.total;
        this.itemList.forEach((item) => {
          if(item.reportAddress){
            item.reportSchedule = 100;
            item.limsSchedule = "完结";
          } else if(item.limsSchedule){ //存在实验阶段且不存在报告
            switch (item.limsSchedule) {
              case "运营":
                item.reportSchedule = 30;
                break;
              case "提取":
                item.reportSchedule = 35;
                break;
              case "扩增":
                item.reportSchedule = 40;
              case "建库":
                item.reportSchedule = 60;
                break;
              case "生信":
                item.reportSchedule = 80;
                item.limsSchedule = "报告审核";
                break;
              case "报告":
                item.reportSchedule = 90;
                item.limsSchedule = "数据分析";
                break;
            }
          } else{ //为空，则代表收样
            item.limsSchedule = "收样";
            item.reportSchedule = 10;
          }
        });
        this.loading = false;
      });
    },

    typeClick(id, sampleDecWay) {
      if(id < 5){
        this.type = id;
        this.queryParams.pageNum = 1;
        this.queryParams.pageSize = 10;
        this.queryParams.sampleDecWay = sampleDecWay;
        this.getList();
      }else{
        this.schedule.open = true;
      }

    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
  },
};
</script>

<style scoped lang="scss">
.home {
  width: 100%;
  height: calc(100% - 50px);
  // background: #f2f3f4;
  .concent {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    background: #ffffff;
    border-radius: 6px 6px 6px 6px;
    opacity: 1;
    box-sizing: border-box;
       padding: 10px 20px;

    .toprow {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      h2 {
        font-weight: bold;
        color: #1f2329;
        font-size: 20px;
        margin: 10px 0 18px 0 !important;
      }
    }
  }
  .rowbtn {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      display: flex;
      .sambtn {
        width: 84px;
        height: 32px;
        border-radius: 4px 4px 4px 4px;
        opacity: 1;
        border: 1px solid #d0d3d6;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 13px;
        cursor: pointer;
        img {
          width: 16px;
          height: 16px;
          margin-right: 10px;
        }
      }
      .button {
        height: 34px;
        background: #efefef;
        color: #595959;
        cursor: pointer;
      }
      .button:hover {
        background: #aa2299;
        color: #ffffff;
      }
      .active {
        background: #aa2299;
        color: #ffffff;
      }
    }
    .right {
      width: 400px;
      height: 34px;
      display: flex;
      .search {
        background: #aa2299;
        color: #ffffff;
        margin-left: 10px;
      }
      .reset {
        background: none;
        color: #1f2329;
      }
    }
  }
  ::v-deep.is-checked {
    .el-checkbox__inner {
      border-color: #aa2290;
      background: #aa2290;
    }
  }
  ::v-deep.el-input.is-focus .el-input__inner {
    border-color: #aa2290 !important;
    box-shadow: 0 0 3px #aa2290 !important;
  }
  ::v-deep.el-card {
    border: none;
  }
  .body {
    height: 100%;
    display: flex;
    flex-direction: column;
    margin-top: 10px;
  }
  .pagefy {
    position: fixed;
    bottom: 20px;
    right: 0;
    color: #1f2329;
  }
  ::v-deep.el-icon-arrow-down {
    font-size: 20px;
  }
  ::v-deep.el-icon-arrow-up {
    font-size: 20px;
  }


  ::v-deep.el-progress-bar__innerText {
    color: #fff !important;
  }
}
</style>
